package com.hxb.api.web.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.hxb.api.dao.ApiLogDao;
import com.hxb.api.entity.ApiLog;
import com.hxb.util.DateUtil;
import com.hxb.util.HttpUtils;
import com.hxb.util.exception.HttpException;
import com.hxb.util.http.RequestParams;
import com.hxb.util.http.Response;
import com.hxb.util.http.ResponseStream;
import com.hxb.util.http.client.HttpRequest;

/**
 * 将浏览器过来的请求转发给目标服务器组件进行处理
 * 
 * @author MARK
 *
 */
public class RecordApiLogServlet extends HttpServlet {
  
	private Gson gson = null;

	public RecordApiLogServlet() {
		super();
	}
	
	public void init() throws ServletException {
		gson = new Gson();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		gson = null; 
	}
	
	@Override
	public void service(ServletRequest arg0, ServletResponse arg1)
			throws ServletException, IOException {
		HttpServletRequest request = (HttpServletRequest)arg0;
		HttpServletResponse response = (HttpServletResponse)arg1;
		String apiId = request.getParameter("apiId");
		String apiName = request.getParameter("apiName");
		String apiUrl = request.getParameter("apiUrl");
		String totalCostTime = request.getParameter("totalCostTime");
		String serverCostTime = request.getParameter("serverCostTime");
		String ajaxCostTime = request.getParameter("ajaxCostTime");
		String ipAddr = getRemoteAddr(request);
		ApiLog log = new ApiLog();
		log.setApiId(Integer.parseInt(apiId));
		log.setApiName(apiName);
		log.setApiUrl(apiUrl);
		log.setIpAddr(ipAddr);
		log.setAccessTime(DateUtil.format());
		log.setTotalCostTime(Integer.parseInt(totalCostTime));
		log.setServerCostTime(Integer.parseInt(serverCostTime));
		log.setAjaxCostTime(Integer.parseInt(ajaxCostTime));
		ApiLogDao  apiLogDao = new ApiLogDao();
		boolean flag = apiLogDao.insert(log);
		String result = null;
		if(flag){
			result = gson.toJson(new Response("0", "日志保存成功"));
		}else{
			result = gson.toJson(new Response("1", "日志保存失败"));
		}
		renderJson(response, result);
	}
	
	/**
	 * 向客户端输出JSON数据
	 * @param response
	 * @param str
	 */
	public static void renderJson(HttpServletResponse response,String str) {	
		response.setContentType("application/json;charset=UTF-8");   
		response.setHeader("Cache-Control","no-cache"); 
		PrintWriter out = null;	
		try {
			out = response.getWriter();
			out.write(str);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.close();			
		}
	}
	
	/**
	 * 获得用户远程地址
	 */
	public static String getRemoteAddr(HttpServletRequest request){
		String remoteAddr = request.getHeader("X-Real-IP");
        if (StringUtils.isNotBlank(remoteAddr)) {
        	remoteAddr = request.getHeader("X-Forwarded-For");
        }else if (StringUtils.isNotBlank(remoteAddr)) {
        	remoteAddr = request.getHeader("Proxy-Client-IP");
        }else if (StringUtils.isNotBlank(remoteAddr)) {
        	remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }
        return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
	}

}
